#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

using namespace std;

string serverip = "8.210.143.192";
uint16_t port = 8080;

int main()
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        cout << "socket fail, errno:" << errno << ", " << strerror(errno) << endl;
        exit(1);
    }

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    server.sin_port = htons(port);

    string message;
    char buffer[1024];

    while(true)
    {
        getline(cin, message);
        socklen_t len = sizeof(server);
        if(sendto(sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&server, len) < 0)
        {
            cout << "socket fail, errno:" << errno << ", " << strerror(errno) << endl;
            continue;
        }
        ssize_t n = recvfrom(sockfd, buffer, sizeof(buffer), 0, nullptr, nullptr);
        if(n < 0)
        {
            cout << "recv fail, errno:" << errno << ", " << strerror(errno) << endl;
            continue;
        }
        buffer[n] = 0;
        cout << buffer << endl;
    }

    return 0;
}